<html>
<head><meta charset="utf-8"><title>Linking shared objects broken? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html">Linking shared objects broken?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222292150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292150">(Jan 11 2021 at 12:44)</a>:</h4>
<p>Hi,</p>
<p>CC <span class="user-mention" data-user-id="133247">@bjorn3</span> whose work I'm extending.</p>
<p>I'm trying to use a build script to link a shared object:</p>
<div class="codehilite"><pre><span></span><code>      println!(&quot;cargo:rustc-link-search=dylib={}/../ykrt_internal/target/release/&quot;,
          env::current_dir().unwrap().to_str().unwrap());
      println!(&quot;cargo:rustc-link-lib=dylib=ykrt_internal&quot;);
</code></pre></div>
<p>(I know this is weird, linking a rust shared object like that. I'm experimenting with building part of a project and its deps <em>with</em> optimisations, but other parts without)</p>
<p>In the invocation of building the test binary we see:</p>
<div class="codehilite"><pre><span></span><code>cc ... &quot;-L&quot; &quot;dylib=/home/vext01/research/yorick/yk/ykrt/../ykrt_internal/target/release&quot; ... &quot;-lykrt_internal&quot; ...
</code></pre></div>
<p>And the library is in there alright:</p>
<div class="codehilite"><pre><span></span><code>$ ls /home/vext01/research/yorick/yk/ykrt/../ykrt_internal/target/release | grep so
libykrt_internal.so
</code></pre></div>
<p>Yet when I do <code>cargo test</code>, I get:</p>
<div class="codehilite"><pre><span></span><code>  = note: /usr/bin/ld: cannot find -lykrt_internal
          collect2: error: ld returned 1 exit status
</code></pre></div>
<p>So we fail to link the test binary.</p>
<p>This is on Debian 10/x86_64.</p>
<p>Any ideas?</p>



<a name="222292233"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292233" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292233">(Jan 11 2021 at 12:45)</a>:</h4>
<p>What is the full linker invocation?</p>



<a name="222292477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292477">(Jan 11 2021 at 12:48)</a>:</h4>
<p>(You should probably use <code>CARGO_WORKSPACE_DIR</code> instead of <code>env::current_dir</code> by the way as I don't think there are any guarantees concerning the current dir used for build scripts)</p>



<a name="222292481"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292481" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292481">(Jan 11 2021 at 12:48)</a>:</h4>
<p>Before I paste, I've just realised that the failing invocation is of <code>cc</code>, not <code>rustc</code> and that we pass <code>"-L" "dylib=...</code> to <code>cc</code>. That's not right is it?</p>



<a name="222292540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292540">(Jan 11 2021 at 12:48)</a>:</h4>
<p>The <code>dylib=</code> part of <code>rustc-link-search</code> needs to be removed then I think.</p>



<a name="222292736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292736">(Jan 11 2021 at 12:50)</a>:</h4>
<p>That works (now I get a runtime linkage problem, but I know how to fix that).</p>
<p>So the cargo docs are wrong?</p>
<blockquote>
<p>cargo:rustc-link-search=[KIND=]PATH</p>
</blockquote>
<p><a href="https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-searchkindpath">https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-searchkindpath</a></p>



<a name="222292774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292774">(Jan 11 2021 at 12:51)</a>:</h4>
<blockquote>
<p>The optional KIND may be one of dependency, crate, native, framework, or all. See the rustc book for more detail.</p>
</blockquote>



<a name="222292890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Linking%20shared%20objects%20broken%3F/near/222292890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Linking.20shared.20objects.20broken.3F.html#222292890">(Jan 11 2021 at 12:52)</a>:</h4>
<p>Oops. I'd still expect it to choke if an invalid kind is supplied.</p>
<p>So user error after all <span aria-label="stuck out tongue closed eyes" class="emoji emoji-1f61d" role="img" title="stuck out tongue closed eyes">:stuck_out_tongue_closed_eyes:</span> </p>
<p>Thanks</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>